/**
 * 获取地址栏参数
 * @param {参数名} name 
 */
function getUrl(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
}

//弹出modal框
$(document).on("click", '[data-toggle="modal"]', function (e) {
    var _this = $(this),
        targetId = _this.attr("href").substr(1),
        targetMoadl = $("#" + targetId);
    targetMoadl.show(0, function (a) {
        targetMoadl.addClass("in");
    });
    e.preventDefault();
})
//隐藏modal框
function modalHide(e) {
    var _this = $(e),
        targetId = _this.parents(".modal_view").attr("id"),
        targetMoadl = $("#" + targetId);
    targetMoadl.removeClass("in");
    setTimeout(function () {
        targetMoadl.hide();
    }, 500);
}

$(document).on("click", ".go_top", function () {
    $('body,html').animate({
        scrollTop: 0
    }, 300);
})

/**
 * $.dialog.alert({
     content: "提示内容"
    });

    $.dialog.confirm({
        content: "你确定要删除吗",
        callback: "alert(1);$.dialog.close();"
    });
 */
$.dialog = {
    alert: function (set) {
        d = defaults(set);
        $(".dialog").remove();
        $(".mask").remove();
        if (d.mask == true) {
            addMask();
        }
        $("body").append('<div class="dialog" style="width:' + d.width + 'px; height:' + d.height + 'px; position:fixed; top:' + getTop(d.height) + '%; left:' + getLeft(d.width) + '%; z-index:1000;"></div>');
        $(".dialog").append('<div class="dialog_body"><div class="dialog_top">' + d.title + '</div><div class="dialog_main">' + d.content + '</div><div class="dialog_bottom"><span><a value="javascript:;" onclick="' + d.callback + '$.dialog.close();">确定</a></span></div></div>');
    },
    confirm: function (set) {
        d = defaults(set);
        $(".dialog").remove();
        $(".mask").remove();
        if (d.mask == true) {
            addMask();
        }
        $("body").append('<div class="dialog" style="width:' + d.width + 'px; height:' + d.height + 'px; position:fixed; top:' + getTop(d.height) + '%; left:' + getLeft(d.width) + '%; z-index:1000;"></div>');
        $(".dialog").append('<div class="dialog_body"><div class="dialog_top">' + d.title + '</div><div class="dialog_main">' + d.content + '</div><div class="dialog_bottom"><span><a value="javascript:;" onclick="' + d.callback + '$.dialog.close();">确定</a>&nbsp;<a value="javascript:;" onclick="$.dialog.close();">取消</a></span></div></div>');
    },
    close: function (set) {
        d = defaults(set);
        $(".dialog").fadeOut(d.closetime, function () {
            $(".dialog").remove();
        });
        $(".mask").fadeOut(d.closetime, function () {
            $(".mask").remove();
        });
    },
};

function defaults(set) {
    var defaults = {
        width: 380,
        height: 150,
        opentime: 280,
        closetime: 280,
        mask: true,
        title: '消息',
        content: null,
        callback: '',
        url: null,
        data: null,
    };
    return $.extend(defaults, set);
}

function addMask() {
    $("body").append('<div class="mask" style="width:100%; height:100%; position:fixed; top:0; left:0; z-index:999;" onclick="$.dialog.close();"></div>');
}

function getLeft(width) {
    return 50 - 50 * width / $(document).width();
}

function getTop(height) {
    return 50 - 50 * height / $(document).height();
}

; (function () {
    function countDown(dom, callback) {
        var _this = this,
            time = dom.data('time');
        if (time <= 0) {
            dom.html('<span class="">00</span><span>00</span><span>00</span><span>00</span></p>');
        } else if (time > 0) {
            dom.html(_this.toStr(time));
            time--;
            _this.timing(time, dom, callback);
        }
    };
    countDown.prototype.timing = function (time, d, callback) {
        var _this = this;
        var t = window.setInterval(function () {
            if (time == 0) {
                clearInterval(t);
                d.html('<p><span>00</span><span>00</span><span>00</span><span>00</span></p>');
                if (callback) {
                    callback(d);
                }
                return false;
            }
            d.html(_this.toStr(time));
            time--;
        }, 1000)
    }
    countDown.prototype.haomiao = function (n) {
        if (n < 10) return "00" + n.toString();
        if (n < 100) return "0" + n.toString();
        return n.toString();
    }
    countDown.prototype.toStr = function (time) {
        var day = 0,
            hour = 0,
            minute = 0,
            second = 0,
            hm = 0;
        if (time > 0) {
            day = Math.floor(time / (60 * 60 * 24)
            );
            hour = Math.floor(time / (60 * 60)
            ) - (day * 24);
            minute = Math.floor(time / 60
            ) - (day * 24 * 60) - (hour * 60);
            second = Math.floor(time
            ) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
            //hm=this.haomiao(time%1000);
        }
        if (day >= 99) day = 99;
        if (day <= 9) day = '0' + day;;
        if (hour <= 9) hour = '0' + hour;
        if (minute <= 9) minute = '0' + minute;
        if (second <= 9) second = '0' + second;
        var str = '<span>' + day + '</span><span>' + hour + '</span><span>' + minute + '</span><span>' + second + '</span>';
        return str;
    };

    countDown.init = function (dom, callback) {
        var _this = this;
        dom.each(function () {
            new _this($(this), callback);
        })
    };
    window["countDown"] = countDown;
})();
